package com.ys.web.sys.notice.service.impl;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.IdUtil;
import com.ys.base.BaseConstants;
import com.ys.base.EnumType;
import com.ys.utils.base.j2se.DateUtil;
import com.ys.utils.base.j2se.StringHandler;
import com.ys.utils.help.GroupUtils;
import com.ys.utils.help.UserUtils;
import com.ys.utils.spring.config.SystemError;
import com.ys.web.sys.notice.mapper.SystemNoticeMapper;
import com.ys.web.sys.notice.po.SystemNoticePo;
import com.ys.web.sys.notice.service.NoticeService;
import com.ys.web.sys.notice.vo.SystemNoticeVo;
import com.ys.web.ws.bean.MessageResponseBean;
import com.ys.web.ws.manager.ChatObjectManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author quxinjun
 * @version 1.0.0
 * @ClassName NoticeServiceImpl.java
 * @Description TODO
 * @createTime 2021年11月01日 14:06:00
 */
@Slf4j
@Service
public class NoticeServiceImpl implements NoticeService {

    private final String SYS_WINDOW = "SYS_NOTICE";


    @Autowired
    private SystemNoticeMapper systemNoticeMapper;

    @Override
    public List<SystemNoticePo> getSystemNoticeList(String ids) {
        return systemNoticeMapper.getSystemNoticeList(ids.split(","));
    }

    public MessageResponseBean assemblyMessage(SystemNoticeVo systemNoticeVo, Integer code) {
        MessageResponseBean messageResponseBean = new MessageResponseBean();
        messageResponseBean.setCode(code);
        messageResponseBean.setFrom(SYS_WINDOW);
        messageResponseBean.setWindowId(SYS_WINDOW);
        messageResponseBean.setUserName(UserUtils.SYS_NOTICE.getNickName());
        messageResponseBean.setAvatarUrl(UserUtils.SYS_NOTICE.getAvatarUrl());
        messageResponseBean.setGroupName(systemNoticeVo.getTitle());
        messageResponseBean.setData(systemNoticeVo.getContent());
        messageResponseBean.setMessageId(systemNoticeVo.getId());
        messageResponseBean.setCreateTime(systemNoticeVo.getCreateTime());
        return messageResponseBean;
    }

    @Override
    public void sendOverdueBackNotice(SystemNoticeVo systemNoticeVo) {
        systemNoticeVo.setId(IdUtil.randomUUID().replaceAll("-", ""));
        systemNoticeVo.setCreateTime(DateUtil.getDate(new Date(), BaseConstants.BASE_FORMAT));
        int i = systemNoticeMapper.addNotice(systemNoticeVo);
        if (i > 0) {
            MessageResponseBean messageResponseBean = this.assemblyMessage(systemNoticeVo, EnumType.MessageCode.SYS_NOTICE.getCode());
            //发送通知
            GroupUtils.sendIfAbsent(messageResponseBean, StringHandler.getString(systemNoticeVo.getUserId()), SYS_WINDOW);
        }
    }


    @Override
    public void addNotice(SystemNoticeVo systemNoticeVo) {
        int i = systemNoticeMapper.addNotice(systemNoticeVo);
        if (i > 0) {
            MessageResponseBean messageResponseBean = this.assemblyMessage(systemNoticeVo, EnumType.MessageCode.SYS_NOTICE.getCode());

            if (systemNoticeVo.getIsAll()) {
                //发送所有人
                ThreadUtil.execAsync(() -> {
                    List<String> ids = systemNoticeMapper.getAllUserId();
                    if (!ids.isEmpty()) {
                        ids.forEach(id -> GroupUtils.sendIfAbsent(messageResponseBean, id, SYS_WINDOW));
                    }
                });
            } else {
                //发送在线的人
                ThreadUtil.execAsync(() -> {
                    List<String> collect = new ArrayList<>(ChatObjectManager.getConnectionMap().keySet());
                    collect.forEach(id -> GroupUtils.sendCurrentOnLine(messageResponseBean, id));
                });
            }
        } else {
            SystemError.wrapBs("500", "操作失败");
        }
    }

    @Override
    public Map<String, Object> getUserSystemNotice() {
        return systemNoticeMapper.getUserSystemNotice();
    }

    @Override
    public void backgroundUserAddNotice(SystemNoticeVo systemNoticeVo) {
        int i = systemNoticeMapper.addNotice(systemNoticeVo);
        if (i > 0) {
            MessageResponseBean messageResponseBean = this.assemblyMessage(systemNoticeVo, EnumType.MessageCode.SYS_NOTICE.getCode());
            //发送群主管理人员
            List<String> ids = systemNoticeMapper.groundAdminUserId();
            if (!ids.isEmpty()) {
                ids.forEach(id -> GroupUtils.sendIfAbsent(messageResponseBean, id, SYS_WINDOW));
            }
        } else {
            SystemError.wrapBs("500", "操作失败");
        }
    }
}
